package com.coai.message.controller

import com.coai.common.dto.ApiResponse
import com.coai.message.dto.*
import com.coai.message.service.ConversationService
import jakarta.validation.Valid
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/conversations")
class ConversationController(
    private val conversationService: ConversationService
) {
    
    @GetMapping
    fun getUserConversations(
        @AuthenticationPrincipal userId: String
    ): ApiResponse<List<ConversationDto>> {
        val conversations = conversationService.getUserConversations(userId)
        return ApiResponse.success(conversations)
    }
    
    @PostMapping
    fun createConversation(
        @Valid @RequestBody request: CreateConversationRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ConversationDto> {
        val conversation = conversationService.createConversation(request, userId)
        return ApiResponse.success(conversation, "创建成功")
    }
    
    @GetMapping("/{conversationId}")
    fun getConversation(
        @PathVariable conversationId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ConversationDto> {
        val conversation = conversationService.getConversation(conversationId, userId)
        return ApiResponse.success(conversation)
    }
    
    @PutMapping("/{conversationId}")
    fun updateConversation(
        @PathVariable conversationId: String,
        @Valid @RequestBody request: UpdateConversationRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ConversationDto> {
        val conversation = conversationService.updateConversation(conversationId, request, userId)
        return ApiResponse.success(conversation, "更新成功")
    }
    
    @PostMapping("/{conversationId}/participants")
    fun addParticipants(
        @PathVariable conversationId: String,
        @Valid @RequestBody request: AddParticipantsRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ConversationDto> {
        val conversation = conversationService.addParticipants(conversationId, request, userId)
        return ApiResponse.success(conversation, "添加成功")
    }
    
    @DeleteMapping("/{conversationId}/participants/{targetUserId}")
    fun removeParticipant(
        @PathVariable conversationId: String,
        @PathVariable targetUserId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ConversationDto> {
        val conversation = conversationService.removeParticipant(conversationId, targetUserId, userId)
        return ApiResponse.success(conversation, "移除成功")
    }
    
    @PostMapping("/{conversationId}/leave")
    fun leaveConversation(
        @PathVariable conversationId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Unit> {
        conversationService.leaveConversation(conversationId, userId)
        return ApiResponse.success(Unit, "已退出群聊")
    }
    
    @PutMapping("/{conversationId}/mute")
    fun toggleMute(
        @PathVariable conversationId: String,
        @RequestParam muted: Boolean,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Unit> {
        conversationService.toggleMute(conversationId, userId, muted)
        return ApiResponse.success(Unit, if (muted) "已静音" else "已取消静音")
    }
    
    @PutMapping("/{conversationId}/pin")
    fun togglePin(
        @PathVariable conversationId: String,
        @RequestParam pinned: Boolean,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Unit> {
        conversationService.togglePin(conversationId, userId, pinned)
        return ApiResponse.success(Unit, if (pinned) "已置顶" else "已取消置顶")
    }
}
